#include <bits/stdc++.h>
using namespace std;
#define int __int128
#define rep(i, l, r) for (int i = l; i <= r; ++i)
const int N = 1e2 + 5; 
int a, b, n, T, ans, tp, st[N]; 

void chk (__int128 m) {
  int cur = 0; 
  int x = m * a / n; 
  cur += (m * a - n * x) * (m * a - n * x); 
  x = m * b / n; 
  cur += (m * b - n * x) * (m * b - n * x); 
  if(cur == 0) return ; 
  ans = min(ans, cur); 
}
void print (int x) {
  if(!x) return puts("0"), void(); 
  for ( ; x; x /= 10) st[++tp] = x % 10; 
  for ( ; tp; --tp) putchar(st[tp] + '0'); putchar('\n'); 
}

signed main () {
  freopen ("K.in", "r", stdin); 
  freopen ("K.out", "w", stdout); 
  scanf("%lld", &T); 
  while (T--) {
    scanf("%lld%lld%lld", &a, &b, &n); 
    ans = 2e18, ans = ans * ans; 
    rep(i, 0, max(a, b)) {
      chk(i * n / a), chk(i * n / a + 1);
      chk(i * n / b), chk(i * n / b + 1);
    }
    print(ans); 
  }
  return 0; 
}